package com.example.jay10_8;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
// import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageButton mImgWeixin;
    private ImageButton mImgFrd;
    private ImageButton mImgAddress;
    private ImageButton mImgSettings;

    private Fragment mTab01 = new WeixinFragment();
    private Fragment mTab02 = new FrdFragment();
    private Fragment mTab03 = new AddressFragment();
    private Fragment mTab04 = new SettingFragment();

    private FragmentManager fm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 主函数

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initView();
        initFragment();
        initEvent();
        selectfragment(0);

    }


    private void initFragment() {
        /* 在FrameLayout中添加按钮  */

        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();

        transaction.add(R.id.id_content,mTab01);
        transaction.add(R.id.id_content,mTab02);
        transaction.add(R.id.id_content,mTab03);
        transaction.add(R.id.id_content,mTab04);
        transaction.commit();

    }

    private void initView(){
        //LinearLayout mTabWeixin = (LinearLayout) findViewById(R.id.weixin_tab1);
        //LinearLayout mTabFrd = (LinearLayout) findViewById(R.id.friend_tab2);
        //LinearLayout mTabAddress = (LinearLayout) findViewById(R.id.address_tab3);
        //LinearLayout mTabSettings = (LinearLayout) findViewById(R.id.setting_tab4);

        /* 将button设置为对应的ImageButton类型的对象   */

        mImgWeixin = findViewById(R.id.weixin_image_button);
        mImgFrd =  findViewById(R.id.friend_image_button);
        mImgAddress =  findViewById(R.id.address_image_button);
        mImgSettings = findViewById(R.id.setting_image_button);

    }

    private void initEvent(){
        /*
          设置点击监听程序 (设置图片监听为例)
          */
        mImgWeixin.setOnClickListener(this);
        mImgFrd.setOnClickListener(this);
        mImgAddress.setOnClickListener(this);
        mImgSettings.setOnClickListener(this);
    }

    private void hideFragment(FragmentTransaction transaction){
        transaction.hide(mTab01);
        transaction.hide(mTab02);
        transaction.hide(mTab03);
        transaction.hide(mTab04);
    }

    private void selectfragment(int i){
        /*
         * 执行此函数时,将灰色图标设置为其对应的彩色图标。
         * */

        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                transaction.show(mTab01);
                mImgWeixin.setImageResource(R.drawable.weixin_pressed);
                break;
            case 1:
                transaction.show(mTab02);
                mImgFrd.setImageResource(R.drawable.friend_pressed);
                break;
            case 2:
                transaction.show(mTab03);
                mImgAddress.setImageResource(R.drawable.address_pressed);
                break;
            case 3:
                transaction.show(mTab04);
                mImgSettings.setImageResource(R.drawable.setting_pressed);
                break;
            default:
                break;
        }
        transaction.commit();
    }

    @Override
    public void onClick(View v){
        /*
         * 设置点击按钮动作监听,如果点击到对应按钮,执行对应的selectfragment函数,并将其他图标reset为灰色。
         * */
        resetimg();
        switch (v.getId()){
            case R.id.weixin_image_button:
                selectfragment(0);
                break;

            case R.id.friend_image_button:
                selectfragment(1);
                break;

            case R.id.address_image_button:
                selectfragment(2);
                break;

            case R.id.setting_image_button:
                selectfragment(3);
                break;

            default:
                break;
        }

    }


    private void resetimg() {
        /*
         * 切换图片至暗色.
         * */
        mImgWeixin.setImageResource(R.drawable.weixin_normal);
        mImgFrd.setImageResource(R.drawable.friend_normal);
        mImgAddress.setImageResource(R.drawable.address_normal);
        mImgSettings.setImageResource(R.drawable.setting_normal);
    }

}